#include "window_common.h"
#include "gkt_network.h"

#if defined(GKT_CONFIG_NETWORK_ENABLE) && GKT_CONFIG_NETWORK_ENABLE

enum {
	TIMER_ID_TIMEOUT = 0,
	TIMER_ID_DELAY_RETURN,
	TIMER_ID_CANCEL_CONNECT_RESET,
	TIMER_ID_MAX
};

#define WINDOW_NETWORK_CONNECT_DELAY_RETURN_TIME_MS 2000

typedef struct _window_network_connect {
	uint32_t cancel_flag;
	uint32_t play_over;
}window_network_connect_s;
static window_network_connect_s s_window_network_connect;

static void window_network_connect_complete(window_result_e result)
{
	uint32_t voice_flags = GKT_WINDOW_VOICE_F_NOTIFY_END | GKT_WINDOW_VOICE_F_NOTIFY_ERROR;

	gkt_window_timer_clear(TIMER_ID_TIMEOUT);

	if (WINDOW_RESULT_OPERATE_SUCCESS == result)
		gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(operation_success), voice_flags, NULL);
	else if (WINDOW_RESULT_OPERATE_TIMEOUT == result)
		gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(operation_timeout), voice_flags, NULL);
	else if (WINDOW_RESULT_OPERATE_FAILED == result)
		gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(operation_failed), voice_flags, NULL);
	else
		gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(operation_success), voice_flags, NULL);//operate cancel

	gkt_window_timeout_enable("window_network_connect - comlete");
	gkt_window_system_standby_enable();

	s_window_network_connect.play_over = 1;
}
static void window_network_connect_please_wait(void)
{
	uint32_t voice_flags = GKT_WINDOW_VOICE_F_NOTIFY_END | GKT_WINDOW_VOICE_F_NOTIFY_ERROR;
	gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(please_wait), voice_flags, NULL);
}
static int window_network_connect_event_handler(
				uint32_t major_type, uint32_t minor_type, 
				uint32_t id, uint32_t data)
{
	int retval = GKT_EVENT_HANDLE_COMPLETE;

	switch(major_type) {
		case GKT_EVENT_WINDOW:
			if (GKT_WINDOW_ACTIVE == minor_type) {
				s_window_network_connect.cancel_flag = 0;
				s_window_network_connect.play_over = 0;
				gkt_window_system_standby_disable();
				gkt_window_timeout_disable("window_network_connect - active");
				window_common_mask_all();
				retval = gkt_network_connect();
				if ((GKT_SUCCESS != retval) && (GKT_EINPROCESS != retval))
					window_network_connect_complete(WINDOW_RESULT_OPERATE_FAILED);
				else {
					gkt_window_timer_set(TIMER_ID_DELAY_RETURN, WINDOW_NETWORK_CONNECT_DELAY_RETURN_TIME_MS, 0, 0);
					gkt_window_timer_set(TIMER_ID_TIMEOUT, GKT_NETWORK_CONNECT_TIMEOUT_SEC*1000, 0, 0);
					window_network_connect_please_wait();
				}
			}
			break;
		case GKT_EVENT_SYSTEM:
			if (GKT_WINDOW_SYSTEM_NETWORK == minor_type) {
				if (GKT_NETWORK_NOTIFY_TYPE_RUNNING_STATUS == id) {
					if (GKT_NETWORK_RUNNING_ST_CONNECTED == data)
						window_network_connect_complete(WINDOW_RESULT_OPERATE_SUCCESS);
					else if (GKT_NETWORK_RUNNING_ST_CONNECT_FAILED == data)
						window_network_connect_complete(WINDOW_RESULT_OPERATE_FAILED);
					else if (GKT_NETWORK_RUNNING_ST_CONNECT_CANCEL_FAILED == data)
						gkt_window_timer_set(TIMER_ID_CANCEL_CONNECT_RESET, 1000, 0, 0);
					else if (GKT_NETWORK_RUNNING_ST_CONNECT_CANCEL_FORBID == data)
						window_common_mask_all();
					else if (GKT_NETWORK_RUNNING_ST_CONNECT_CANCELED == data) {
						gkt_window_timer_clear(TIMER_ID_CANCEL_CONNECT_RESET);
						window_network_connect_complete(WINDOW_RESULT_MAX);
					}
				}
			}
		case GKT_EVENT_KEY:
			if (GKT_KEY_ASTERISK == minor_type) {
				if (!s_window_network_connect.cancel_flag) {
					retval = gkt_network_connect_cancel();
					if (GKT_SUCCESS == retval)
						s_window_network_connect.cancel_flag = 1;
					else {
						gkt_error("window_network_connect - cancel connect error. retval:%d\n", retval);
						s_window_network_connect.cancel_flag = 0;
					}
				}
				retval = GKT_EVENT_HANDLE_COMPLETE;
			}
			break;
		case GKT_EVENT_TIMER:
			if (TIMER_ID_TIMEOUT == minor_type)
				window_network_connect_complete(WINDOW_RESULT_OPERATE_TIMEOUT);
			else if (TIMER_ID_CANCEL_CONNECT_RESET == minor_type)
				s_window_network_connect.cancel_flag = 0;
			else if (TIMER_ID_DELAY_RETURN == minor_type)
				window_common_mask_except_return();
		case GKT_EVENT_VOICE:
			if (s_window_network_connect.play_over == 1) {
				s_window_network_connect.play_over = 0;
				gkt_window_exit();
			}
			break;
		default:
			break;
	}

	return retval;
}
GKT_WINDOW_DESC_DEFINE_NO_WIDGETS(network_connect);

#endif

